'''
Author: your name
Date: 2021-03-26 09:20:19
LastEditTime: 2021-04-20 11:51:56
LastEditors: Please set LastEditors
Description: In User Settings Edit
FilePath: /tinyms/tinyms-api/apps/auth/dict/service.py
'''
import json
from app import db
from .model import Dict

class DictService():
    '''
    查询字典列表
    '''
    def pages(dict_type,page):
        pages = Dict.query.filter(Dict.dict_type==dict_type).order_by(Dict.dict_level.asc()).paginate(int(page),30,False) #flask_sqlalchemy提供分页方法.paginate
        return pages

    '''
    查询字典一级
    '''
    def pagesyj(page):
        pages_yj = Dict.query.with_entities(Dict.dict_type,Dict.type_name).distinct().paginate(int(page),30,False) #flask_sqlalchemy提供分页方法.paginate
        return pages_yj
        # result  = RiskDataModel.query.with_entities(RiskDataModel.store_st_id).distinct().all()
    
    '''
    保存字典信息
    '''
    def dictSave(dict):
        db.session.add(dict)
        db.session.commit()
    
    def delete(dict_pk):
        dict = Dict.query.get(dict_pk)
        db.session.delete(dict)
        db.session.commit()#必须要有db.session.commit,否则数据无法删除
    
    '''
    查询字典列表
    '''
    def list(dict_status):
        list = Dict.query.filter(Dict.dict_status==dict_status).all()
        return list
    
    def list_all(dict_parent):
        if dict_parent !=None:
            dicts = Dict.query.filter(Dict.dict_parent==dict_parent).all()
        return dicts